package com.prodavalnik.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.prodavalnik.model.DataBaseController;
import com.prodavalnik.model.User;

/**
 * Servlet implementation class ActivityServlet
 */
@WebServlet("/ActivityServlet")
public class ActivityServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ActivityServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter writer = response.getWriter();

		HttpSession session = request.getSession(false);
		
		if(session==null){
			writer.write("error");
		}
		
		String username = (String) session.getAttribute("username");
		String password = (String) session.getAttribute("password");

		User user=null;
		
		try {
			user = DataBaseController.returnUserByUsernameAndPassword(username,password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		int userBought = 0;
		int userSales = 0;
		int userLiked = 0;
		
		try {
			userBought = DataBaseController.getRecordForUserBought(user.getId());
			userSales = DataBaseController.getRecordForUserSales(user.getId());
			userLiked = DataBaseController.getRecordForUserLikedCategories(user.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		writer.println(userSales+","+userBought+","+userLiked+","+username);
		
	}

}
